//
//  goodsListViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/4/3.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "goodsListViewController.h"
#import "configddd.h"
#import <Masonry/Masonry.h>
#import "goodsListCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "shopDetailsViewController.h"

@interface goodsListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextViewDelegate>

@property(nonatomic,strong) UICollectionView *goodsCollectionView;

@end

@implementation goodsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"合作商家";
    [self requestData];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((scrWidth() - screenScale(20)) / 2, screenScale(400));
//    layout.minimumLineSpacing = screenScale(20);
//    layout.minimumInteritemSpacing = screenScale(20);
    
    self.goodsCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), scrHeight() - 64) collectionViewLayout:layout];
}

#pragma mark ------------------------------- init

- (void)setGoodsCollectionView:(UICollectionView *)goodsCollectionView{
    
    _goodsCollectionView = goodsCollectionView;
    _goodsCollectionView.delegate = self;
    _goodsCollectionView.dataSource = self;
    _goodsCollectionView.backgroundColor = getColor(@"ffffff");
    [_goodsCollectionView registerClass:[goodsListCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    [_goodsCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader"];
    [self.view addSubview:_goodsCollectionView];
}

#pragma mark ---------------------------------- requestData

- (void)requestData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: _partnerId forKey:@"type"];
    
    [[MNetworkHandler sharedInstance]conURL:Goods_List
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       dataDic = [returnData objectForKey:@"data"];
                                       [_goodsCollectionView reloadData];
                                       
                                       NSLog(@"==\n\n\n==dataDic====%@",dataDic);
                                       
                                       [hud removeFromSuperview];
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       
                                   }
                                   else{
                                   
                                       [hud removeFromSuperview];
//                                       dataDic = [returnData objectForKey:@"data"];
                                       [_goodsCollectionView reloadData];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

#pragma mark ------------------------------- UICollectionViewDelegate

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader" forIndexPath:indexPath];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(550))];
    [headView addSubview:view];
    
    //顶部图片
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(310))];
    NSString *urlGood = dataDic[@"shop"][@"pic"];
    NSString *urlN = manage(AP_HOST, urlGood);
    NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    NSURL *urlW = [NSURL URLWithString:strUrl];
    [img sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
    [view addSubview:img];
    
    //logo
    UIImageView *logo = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(250), screenScale(120), screenScale(120))];
    NSString *urlGood1 = dataDic[@"shop"][@"pic"];
    NSString *urlN1 = manage(AP_HOST, urlGood1);
    NSString *strUrl1 = [urlN1 stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    NSURL *urlW1 = [NSURL URLWithString:strUrl1];
    [logo sd_setImageWithURL:urlW1 placeholderImage:[UIImage imageNamed:@"niunai"]];
    [view addSubview:logo];
    
    UILabel *nameLable = [[UILabel alloc]initWithFrame:CGRectMake(0, screenScale(185), scrWidth() - screenScale(120) - screenScale(40), screenScale(70))];
    nameLable.text = dataDic[@"shop"][@"name"];
    nameLable.font = get4sFont(14);
    nameLable.textAlignment = NSTextAlignmentCenter;
    [view addSubview:nameLable];
    [nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(logo).with.offset(0);
        make.left.mas_equalTo(logo).with.offset(screenScale(140));
        
        make.height.mas_equalTo(screenScale(60));
    }];
        
    UITextView *text = [[UITextView alloc]init];
    text.editable = NO;
    text.text = dataDic[@"shop"][@"content"];
    text.font = get4sFont(11);
    text.textColor = getColor(@"8a8a8a");
    [view addSubview:text];
    [text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(logo).with.offset(screenScale(145));
        make.left.mas_equalTo(logo).with.offset(screenScale(20));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-25));
        make.bottom.mas_equalTo(view).with.offset(screenScale(screenScale(-25)));
    }];
    
    UIView *threadView = [[UIView alloc]init];
    threadView.backgroundColor = getColor(@"f4f4f4");
    [headView addSubview:threadView];
    [threadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(headView).with.offset(0);
        make.left.mas_equalTo(headView).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(10));
    }];
    
    return headView;
}

// headView 大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    CGSize size={scrWidth(),screenScale(550)};
    return size;
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    shopDetailsViewController *goodsVC = [[shopDetailsViewController alloc]init];
    goodsVC.partnerId = dataDic[@"goods"][indexPath.row][@"id"];
    goodsVC.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:goodsVC animated:YES];
}

#pragma mark ------------------------------- UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [dataDic[@"goods"] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identify = @"identifier";
    goodsListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    
    NSDictionary *dict = dataDic[@"goods"][indexPath.row];
    NSString *urlGood = [dict objectForKey:@"logo"];
    NSString *urlN = manage(AP_HOST, urlGood);
    NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    NSURL *urlW = [NSURL URLWithString:strUrl];
    [cell.imageshow sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
    
    cell.myLabel.text = dataDic[@"goods"][indexPath.row][@"name"];
    
    cell.priceLable.text = [@"¥" stringByAppendingString: dataDic[@"goods"][indexPath.row][@"price"]];
    
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[@"¥" stringByAppendingString:dataDic[@"goods"][indexPath.row][@"original_price"]] attributes:attribtDic];
    cell.beforeLable.attributedText = attribtStr;
    
    return cell;
}

@end
